﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UniRlv.UI.Main.Widget
{
    public partial class FrmMultiItemsEdit : UniRlv.UI.Main.DialogBase
    {
        public string Tip { get; set; }
        public string[] Splitters { get; set; }
        public string Splitter { get; set; }
        public string values { get; set; }

        public FrmMultiItemsEdit()
        {
            InitializeComponent();
        }

        private void FrmMultiItemsEdit_Load(object sender, EventArgs e)
        {
            lblTip.Text = Tip;
            string[] items = values.Split(Splitters, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in items)
            {
                lstValues.Items.Add(item, true);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtInput.Text))
            {
                ShowInfoMessageBox("提示", "输入值不能为空！");
                txtInput.Focus();
                return;
            }

            lstValues.Items.Add(txtInput.Text, true);
            txtInput.Focus();
            txtInput.SelectAll();
        }

        private void btnAllSel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lstValues.Items.Count; i++)
            {
                lstValues.SetItemChecked(i, true);
            }
        }

        private void btnAllDeSel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lstValues.Items.Count; i++)
            {
                lstValues.SetItemChecked(i, false);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            values = string.Empty;

            if (lstValues.CheckedItems.Count>0)
            {
                for (int i = 0; i < lstValues.CheckedItems.Count; i++)
                {
                    values += Splitter;
                    values += lstValues.CheckedItems[i].ToString();
                }

                values = values.Substring(1);
            }
            else
            {
                values = string.Empty;
            }

            Close();
        }

        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnAdd.PerformClick();
            }
        }
    }
}
